import { login } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/assets/logo/logo.png'
import usePermissionStore from '@/store/modules/permission'
import Cookies from 'js-cookie'

const useUserStore = defineStore(
  'user',
  {
    state: () => ({
      token: getToken(),
      id: '',
      name: '',
      avatar: '',
      roles: [],
      permissions: [],
      loginInfo: {}
    }),
    actions: {
      //本地调试
      setLoginToken(token) {
        this.roles = ['admin']
        this.token = token
        this.name = '云创特检'
        this.avatar = defAva;
        this.permissions = ["*:*:*"]
        usePermissionStore().setLocalRouters()
        setToken(token)
      },
      //加载本地所有菜单
      loadLocalRouters() {
        this.permissions = ["*:*:*"]
        usePermissionStore().setLocalRouters()
      },
      // 登录
      login(userInfo) {
        const username = userInfo.username.trim()
        const password = userInfo.password
        const code = userInfo.code
        const uuid = userInfo.uuid
        return new Promise((resolve, reject) => {
          login(username, password, code, uuid).then(res => {
            if (res.data && res.data.token) {
              setToken(res.data.token)
              this.token = res.data.token
              this.id = res.data.id
              this.loginInfo = res.data
              Cookies.set('loginInfo', JSON.stringify(res.data))
              resolve()
            } else {
              Cookies.set('loginInfo', '')
              reject("获取登录信息失败")
            }
          }).catch(error => {
            Cookies.set('loginInfo', '')
            reject(error)
          })
        })
      },
      // 获取用户信息
      getInfo() {
        return new Promise((resolve, reject) => {
          this.id = 1
          this.name = '操作员'
          this.avatar = defAva
          this.roles = ['ROLE_DEFAULT']

          let info = Cookies.get('loginInfo')
          if (info) {
            this.loginInfo = JSON.parse(info)
          } else {
            this.loginInfo = {}
          }
          resolve({})

          // getInfo(this.id).then(res => {
          //   const user = res.data
          //   const avatar = (user.avatar == "" || user.avatar == null)
          //     ? defAva : import.meta.env.VITE_APP_BASE_API + user.avatar;

          //   if (res.roles && res.roles.length > 0) {
          //     this.roles = res.roles
          //     this.permissions = res.permissions
          //   } else {
          //     this.roles = ['ROLE_DEFAULT']
          //   }
          //   this.id = user.id
          //   this.name = user.nickname
          //   this.avatar = avatar
          //   resolve(res)
          // }).catch(error => {
          //   reject(error)
          // })
        })
      },
      // 退出系统
      logOut() {
        return new Promise((resolve, reject) => {
          this.token = ''
          this.roles = []
          this.permissions = []
          removeToken()
          resolve()

          // logout(this.token).then(() => {
          //   this.token = ''
          //   this.roles = []
          //   this.permissions = []
          //   removeToken()
          //   resolve()
          // }).catch(error => {
          //   reject(error)
          // })
        })
      }
    }
  })

export default useUserStore
